package etcd

import (
	"Logger/logAgent/collect"
	"context"
	"encoding/json"
	clientv3 "go.etcd.io/etcd/client/v3"
	"time"
)

var (
	Cli *clientv3.Client
)

func Init(addrs []string) (err error) {
	Cli, err = clientv3.New(clientv3.Config{
		Endpoints:   addrs,
		DialTimeout: time.Second * 5,
	})
	if err != nil {
		return err
	}
	//defer cli.Close()
	return
}

func GetCollections(Key string) (collect.Collections, error) {
	resp, err := Cli.Get(context.Background(), Key)
	if err != nil {
		return nil, err
	}
	var cos collect.Collections
	for _, ev := range resp.Kvs {
		err := json.Unmarshal(ev.Value, &cos)
		if err != nil {
			return nil, err
		}
	}
	return cos, nil
}
